layui.define(function(exports) {
	"use strict";

	var Select = function(options) {
		this.options = options;
		this.elem = options.elem;
		this.url = options.url;
		this.method = options.method;
		this.data = options.data || [];
		this.defVal = options.defVal || '';
	}

	Select.prototype.init = function() {
		var html = this.getHtml();
		$(this.elem).html(html);
	}

	Select.prototype.getHtml = function() {
		var that = this;
		var optionsHtml = '';
		$.ajax({
			url : that.url,
			type : that.method,
			dataType:"json",
			data:that.data,
			async:false,
			success:function(response){
				var data = response.data;
				var html = '<option value=""></option>';
				$.each(data,function(i,item){
					if(that.defVal == item.value){
						html += '<option value="'+item.value+'" selected="selected">'+item.text+'</option>';
					}else{
						html += '<option value="'+item.value+'">'+item.text+'</option>';
					}
					
				});
				optionsHtml = html;
			}
		});
		return optionsHtml;
	}

	exports('select', function(options) {
		var select = new Select(options = options || {});
		select.init();
	});
});